/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.app.web.controllers;

import java.util.LinkedList;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.app.model.entities.JmbPostTags;
import org.app.model.entities.JmbUserPosts;
import org.app.model.session.JmbPostTagsFacade;
import org.app.model.session.JmbUserPostsFacade;
import org.jblack.util.MeanProcesor;
import org.jblack.util.Word;

/**
 *
 * @author Santiago
 */
@ManagedBean
@RequestScoped
public class UserWallController {
  @EJB
  private JmbPostTagsFacade jmbPostTagsFacade;
  @EJB
  private JmbUserPostsFacade jmbUserPostsFacade;
  private String userPost;
  private LinkedList<Word> tags;
  private FacesContext facesContext;

  /**
   * Creates a new instance of PostWallController1
   */
  public UserWallController() {
    facesContext = FacesContext.getCurrentInstance();
  }

  public void postShare() {
    
    
    LoginController  loginController = (LoginController) facesContext.getExternalContext().getSessionMap().get("loginController");
    
    
    if (userPost.length() > 2) {
      JmbUserPosts Upost = new JmbUserPosts();
      Upost.setPostUserId(loginController.getJmbUser().getUserMail()); 
      Upost.setPostDescription(userPost);
      Upost.setPostUserJnick(loginController.getJmbUser().getUserJnick());
      
      jmbUserPostsFacade.create(Upost);
      tags = new LinkedList<Word>();
      MeanProcesor objProcess = new MeanProcesor();
      this.tags = (LinkedList<Word>) objProcess.paragraphTags(this.userPost);      
      
      for (int i = 0; i < this.tags.size(); i++) {       
        
        System.out.println(this.tags.get(i).getWord() + ":" + this.tags.get(i).getRoot());
        
        
      }
      for (int i = 0; i < 3; i++) {
        JmbPostTags tag = new JmbPostTags();
        tag.setTagPostId(Upost.getPostId());
        tag.setTagName(this.tags.get(i).getWord());
        jmbPostTagsFacade.create(tag);
        
        
        
      }
      userPost="";
    }
  }

  public void moreBean(String idPost){
    JmbUserPosts post =  jmbUserPostsFacade.find(Integer.parseInt(idPost));
    if(post.getPostBeans() == null){
      post.setPostBeans(1);
      jmbUserPostsFacade.edit(post);
    }else{
      int x = post.getPostBeans();
      post.setPostBeans(x+1);
      jmbUserPostsFacade.edit(post);
    }
  }
  /**
   * @return the userPost
   */
  public String getUserPost() {
    return userPost;
  }

  /**
   * @param userPost the userPost to set
   */
  public void setUserPost(String userPost) {
    this.userPost = userPost;
  }

  /**
   * @return the tags
   */
  /**
   * @param tags the tags to set
   */
  public void setTags(LinkedList<Word> tags) {
    this.tags = tags;
  }
}
